package com.mioto.ld.module.knowledge.controller;

import cn.hutool.core.util.StrUtil;
import com.mioto.ld.module.knowledge.model.KnowledgeBasePagerVO;
import org.springframework.web.bind.annotation.*;
import com.mioto.ld.module.knowledge.model.KnowledgeBase;
import com.mioto.ld.module.knowledge.service.IKnowledgeBaseService;
import com.mioto.ld.result.ResultData;
import com.mioto.ld.result.SystemTip;
import com.mioto.ld.component.BasePager;
import com.mioto.ld.exception.BasicException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 *
 * @author mioto
 * @date 2024-05-14 19:19:36
 */
@RestController
@RequestMapping("knowledgeBase")
@Api(tags = "知识库管理")
public class KnowledgeBaseController {
    @Resource
    private IKnowledgeBaseService knowledgeBaseService;

    @GetMapping
    @ApiOperation(value="查询知识库列表",response = KnowledgeBase.class)
    public ResultData list (HttpServletRequest request,KnowledgeBase knowledgeBase){
        return ResultData.success(knowledgeBaseService.findList(knowledgeBase));
    }

    @GetMapping("/pager")
    @ApiOperation(value="分页查询知识库",response = KnowledgeBasePagerVO.class)
    public ResultData pager (HttpServletRequest request, KnowledgeBase knowledgeBase,BasePager basePager){
        PageHelper.startPage(basePager.getPage(), basePager.getRows(), basePager.getSortBy());
        List<KnowledgeBasePagerVO> list = knowledgeBaseService.findPager(knowledgeBase);
        PageInfo<KnowledgeBasePagerVO> pageInfo = new PageInfo<>(list);
        Map<String, Object> result = new HashMap<>(4);
        result.put("count", pageInfo.getTotal());
        result.put("list", pageInfo.getList());
        return ResultData.success(result);
    }

    @GetMapping("/{column}/{value}")
    @ApiOperation(value="通过字段名查询知识库",response = KnowledgeBase.class)
    public ResultData findByColumn (@PathVariable("column")String column,@PathVariable("value")Object value){
        return ResultData.success(knowledgeBaseService.findByColumn(StrUtil.toUnderlineCase(column),value));
    }

    @PostMapping
    @ApiOperation(value="新增知识库")
    public ResultData add (KnowledgeBase knowledgeBase){
        return Optional.of(knowledgeBaseService.insert(knowledgeBase))
                .filter(count -> count > 0)
                .map(count -> ResultData.success(knowledgeBase))
                .orElseThrow(() -> new BasicException(SystemTip.INSERT_FAIL));
    }

    @PatchMapping
    @ApiOperation(value="更新知识库 局部更新,忽略空值")
    public ResultData updateIgnoreNull (KnowledgeBase knowledgeBase){
        return Optional.of(knowledgeBaseService.updateIgnoreNull(knowledgeBase))
                .filter(count -> count > 0)
                .map(count -> ResultData.success(knowledgeBase))
                .orElseThrow(() -> new BasicException(SystemTip.UPDATE_FAIL));
    }

    @PutMapping
    @ApiOperation(value="更新知识库 整体更新")
    public ResultData update (KnowledgeBase knowledgeBase){
        return Optional.of(knowledgeBaseService.update(knowledgeBase))
                .filter(count -> count > 0)
                .map(count -> ResultData.success(knowledgeBase))
                .orElseThrow(() -> new BasicException(SystemTip.UPDATE_FAIL));
    }

    @DeleteMapping("/{column}/{value}")
    @ApiOperation(value="通过字段名删除知识库")
    public ResultData deleteByColumn (@PathVariable("column")String column,@PathVariable("value")Object value){
        return Optional.of(knowledgeBaseService.deleteByColumn(StrUtil.toUnderlineCase(column),value))
                .filter(count -> count > 0)
                .map(count -> ResultData.success())
                .orElseThrow(() -> new BasicException(SystemTip.DELETE_FAIL));
    }

    @DeleteMapping("/batch")
    @ApiOperation(value="批量删除知识库")
    public ResultData batchDelete (Integer... ids){
        return Optional.of(knowledgeBaseService.batchDelete(ids))
                .filter(count -> count > 0)
                .map(count -> ResultData.success())
                .orElseThrow(() -> new BasicException(SystemTip.DELETE_FAIL));
    }

    @GetMapping("/batch")
    @ApiOperation(value="批量查询知识库")
    public ResultData batchFind (Integer... ids){
        return  ResultData.success(knowledgeBaseService.batchFind(ids));
    }
}